<template>
  <div>
    <!-- 面包屑头部导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 设置gutter每一列之间距离 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
          v-model="queryData.query" 
          clearable 
          @clear="getGoods"
          placeholder="请输入商品">
            <el-button @click="getGoods" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="5">
          <el-button type="primary" @click="goAddGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price" width="80"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="80"></el-table-column>
        <el-table-column label="创建时间" width="150">
          <template slot-scope="scope">{{scope.row.add_time | dataFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="130">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="delGoods(scope.row.goods_id)" size="mini" type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryData: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      goodsList: [],
      total: 0,
      goods_name: "test_goods_name2",
      goods_cat: "1,2,3",
      goods_price: 20,
      goods_number: 30,
      goods_weight: 40,
      goods_introduce: "abc",
      pics: [{ pic: "/tmp_uploads/30f08d52c551ecb447277eae232304b8" }],
      attrs: [
        {
          attr_id: 15,
          attr_value: "ddd"
        },
        {
          attr_id: 15,
          attr_value: "eee"
        }
      ]
    };
  },

  methods: {
    async getGoods() {
      var res = await this.axios.get("goods", {
        params: this.queryData
      });
      if (res.meta.status == 200) {
        this.goodsList = res.data.goods;
        this.total = res.data.total;
      }
      console.log(this.goodsList);
      console.log(this.total);
    },

    // 每页多少条发生变化时触发
    handleSizeChange(val) {
    //   console.log(val);
      this.queryData.pagesize = val;
      this.getGoods();
    },
    // 页码发生变化时触发
    handleCurrentChange(val) {
    //   console.log(val);
      this.queryData.pagenum = val;
      this.getGoods();
    },

    // 添加商品
    async goAddGoods() {
      this.$router.push('/goods/add')
    },


    // 删除商品
    delGoods(id){
        this.$cfm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
        //   console.log("确认删除");
          var res = await this.axios.delete("goods/" + id);
          if (res.meta.status == 200) {
            this.$mg.success("删除成功");
            this.getGoods();
          } else {
            this.$mg.error(res.meta.msg);
          }
        })
        .catch(() => {
          this.$mg.info("已取消删除");
        });
    }
  },

  created() {
    this.getGoods();
  }
};
</script>

<style lang='less' scoped>
</style>
